<?php
/**
 * 工程师管理
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/6/29
 * Time: 17:56
 */
namespace app\admin\controller;
use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\repository\Repository;
use think\Request;
use think\Validate;

class Engin extends Common
{
    /**
     * 列表页面
     * @return mixed
     */
    public function listUI(){
        return $this->fetch('list');
    }

    /**
     *
     * @param Request $request
     * @return array
     */
    public function query(Request $request){
        $input = $request->param();
        if(empty($input['gid'])) return ['code'=>1,'msg'=>'gid参数错误'];
        $rows = db('admin')->alias('a')->where(function ($query)use($input){
            if(!empty($input['gid']))
                $query->where('a.group_id',$input['gid']);
            if(isset($input['is_part']))
                $query->where('a.is_part',$input['is_part']);
            if(!empty($input['aid']))
                $query->where('a.admin_id',$input['aid']);
            if(!empty($input['apid']))
                $query->where('a.a_pid',$input['apid']);
            if(!empty($input['key']))
                $query->where('a.name|a.tel|a.email|a.username',$input['key']);
            if(isset($input['is_group']))
                $query->where('a.is_group',$input['is_group']);
        })
            ->join('auth_group ag','a.group_id=ag.group_id','left')
            ->join('jobs js','js.id=a.jobs_id','left')
            ->join('admin ac','ac.admin_id=a.a_pid','left')
            ->field(['a.*,ag.title','js.title as jobs_title','ac.name as p_name'])
            ->paginate($input['limit'])->toArray();
        return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$rows['data'],'count'=>$rows['total'],'rel'=>1];
    }

    /**
     * 获取队员列表
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function teams(Request $request){
        $input = $request->param();
        $info = db('admin')->where('admin_id',$input['aid'])->find();

        $rows = db('admin')->alias('a')->where(function ($query)use($info){
            $query->where('a.group_id',5);
            if($info['is_part']==1){
                $query->where('a.a_pid',$info['a_pid']);
            }
            $query->where('a.is_part',$info['is_part']);
        })
            ->join('auth_group ag','a.group_id=ag.group_id','left')
            ->join('jobs js','js.id=a.jobs_id','left')
            ->join('admin ac','ac.admin_id=a.a_pid','left')
            ->field(['a.*,ag.title','js.title as jobs_title','ac.name as p_name'])
            ->select();
        return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$rows,'rel'=>1];
    }
    /**
     * 添加页面
     * @return mixed
     */
    public function addUI(){

        if(request()->isPost()){
            $data = input('post.');
            $check_user = Admin::get(['username'=>$data['username']]);
            if ($check_user) {
                return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
            }
            $data['pwd'] = input('post.pwd', '', 'md5');
            $data['add_time'] = time();
            $data['ip'] = request()->ip();
            $data['group_id'] = 5;
            //验证
            $msg = $this->validate($data,'app\admin\validate\Admin');
            if($msg!='true'){
                return json(['code'=>0,'msg'=>$msg]);
            }
            //单独验证密码
            $checkPwd = Validate::make([input('post.pwd')=>'require']);
            if (false === $checkPwd) {
                return json(['code'=>0,'msg'=>'密码不能为空！']);
            }
            //添加
            if (Admin::create($data)) {
                return json(['code'=>1,'msg'=>'添加成功!','url'=>url('listUI')]);
            } else {
                return json(['code'=>0,'msg'=>'添加失败!']);
            }
        }
        /*$auth_group=db('auth_group')->select();
        $this->assign('authGroup',$auth_group);*/
        $this->assign('info','null');
        $this->assign('selected', 'null');
        return $this->fetch('add');
    }

    /**
     * 编辑页面
     */
    public function editUI(){
        if(request()->isPost()){
            $data = input('post.');
            $pwd=input('post.pwd');
            $map[] = ['admin_id','<>',$data['admin_id']];
            $where['admin_id'] = $data['admin_id'];

            if($data['username']){
                $map[] = ['username','=',$data['username']];
                $check_user = Admin::where($map)->find();
                if ($check_user) {
                    return $result = ['code'=>0,'msg'=>'用户已存在，请重新输入用户名!'];
                }
            }
            if ($pwd){
                $data['pwd']=input('post.pwd','','md5');
            }else{
                unset($data['pwd']);
            }
            $msg = $this->validate($data,'app\admin\validate\Admin');
            if($msg!='true'){
                return $result = ['code'=>0,'msg'=>$msg];
            }
            Admin::update($data,$where);
            session('username',$data['username']);
            $avatar = $data['avatar']==''?'/public/static/admin/images/0.jpg':$data['avatar'];
            session('avatar',$avatar);
            return $result = ['code'=>1,'msg'=>'管理员修改成功!','url'=>url('listUI')];
        }else{
            $admin = new Admin();
            $info = $admin->getInfo(input('admin_id'));
            $this->assign('info', json_encode($info,JSON_UNESCAPED_UNICODE));
            $this->assign('title',lang('edit').lang('admin'));
            return $this->fetch('add');
        }
    }

    public function adminState(){
        $id=input('post.id');
        $is_open=input('post.is_open');
        if (empty($id)){
            $result['status'] = 0;
            $result['info'] = '用户ID不存在!';
            $result['url'] = url('adminList');
            return $result;
        }
        db('admin')->where('admin_id='.$id)->update(['is_open'=>$is_open]);
        $result['status'] = 1;
        $result['info'] = '用户状态修改成功!';
        $result['url'] = url('adminList');
        return $result;
    }


    public function adminWork(){
        $id=input('post.admin_id');
        if(empty($id)) return ['code'=>0,'msg'=>'参数错误'];
        $row = db('admin')->where('admin_id='.$id)->update(['is_at'=>0]);
        if($row)
            return ['code'=>1,'msg'=>'操作成功'];
        return ['code'=>0,'msg'=>'操作失败'];
    }

    /**
     * 组长设置取消
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function isgroup(){
        $id=input('post.admin_id');
        $gr=input('post.is_group');
        if(empty($id)) return ['code'=>0,'msg'=>'参数错误'];
        $row = db('admin')->where('admin_id='.$id)->update(['is_group'=>$gr]);
        if($row)
            return ['code'=>1,'msg'=>'操作成功'];
        return ['code'=>0,'msg'=>'操作失败'];
    }
}